package view;

import algorithme.*;
import model.Arc;
import model.Graph;
import model.Noeud;
import parser.GraphFilePxPosition;
import test.Application;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Set;

public class GUIHelper {

    public static void showOnFrame(JComponent component, String frameName) {
        final JFrame frame = new JFrame(frameName);

        ButtonGroup buttonGroupAlgorithmes  = new ButtonGroup();
        JToolBar jToolBarAlgorithmes = new JToolBar();
        final JButton jButtonExecuteAlgorithme = new JButton("Execute");

        final JButton jButtonGenerateGraph = new JButton("Generate");

        final JButton jButtonAfficheLigne = new JButton("Affiche ligne");

        JButton jButtonSource = Application.getWindowS().getjButtonSource();
        JButton jButtonDestination = Application.getWindowS().getjButtonDestination();

        JToolBar jToolBarLignes = new JToolBar();
        JScrollPane scrollPaneLignes = new JScrollPane(jToolBarLignes);

        JToolBar jToolBarHistory = Application.getWindowS().getjToolBarHistory();

        JPanel pane = new JPanel();
        BorderLayout bord = new BorderLayout();

        final JRadioButton jRadioButtonBellman = new JRadioButton("Bellman & Ford");
        final JRadioButton jRadioButtonDijkstra = new JRadioButton("Dijkstra");
        final JRadioButton jRadioButtonExploration = new JRadioButton("Exploration arborescente");

        WindowAdapter wa = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                try {
                    GraphFilePxPosition.save(Application.getGraphS());
                } catch (Exception e1) {
                    e1.printStackTrace();
                    System.exit(0);
                }
                System.exit(0);
            }
        };
        //        frame.setSize(dim);
        frame.addWindowListener(wa);

        jButtonExecuteAlgorithme.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Application.getGraphS().affichePlusCourtChemin();
            }
        });

        jButtonGenerateGraph.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String nbNoeud = JOptionPane.showInputDialog("Combien de noeud?");
                Application.getInstance().generateGraph(Integer.valueOf(nbNoeud));
            }
        });

        jButtonAfficheLigne.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Graph graph = Application.getGraphS();
                Set<String> lignes = graph.getArcs().keySet();
                lignes.remove("0");
                Object[] possibilities = lignes.toArray();
                String ligne = (String)JOptionPane.showInputDialog(
                        frame,
                        "Quel ligne?",
                        "Customized Dialog",
                        JOptionPane.PLAIN_MESSAGE,
                        null,
                        possibilities,
                        null);
                Application.getInstance().getGraph().afficheLigne(ligne);
            }
        });

        jButtonSource.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Graph graph = Application.getGraphS();
                Object[] possibilities = graph.getNoeuds().values().toArray();
                Noeud s = (Noeud)JOptionPane.showInputDialog(
                        frame,
                        "Source: ",
                        "Customized Dialog",
                        JOptionPane.PLAIN_MESSAGE,
                        null,
                        possibilities,
                        null);
                graph.setSource(s);
            }
        });

        jButtonDestination.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Graph graph = Application.getGraphS();
                Object[] possibilities = graph.getNoeuds().values().toArray();
                Noeud s = (Noeud)JOptionPane.showInputDialog(
                        frame,
                        "Destination: ",
                        "Customized Dialog",
                        JOptionPane.PLAIN_MESSAGE,
                        null,
                        possibilities,
                        null);
                graph.setDestination(s);
            }
        });

        jRadioButtonBellman.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                if (jRadioButtonBellman.isSelected()) {
                    Application.getGraphS().setCurrentAlgorithme(new AlgorithmeBellmanFord());
                }
            }
        });

        jRadioButtonDijkstra.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                if (jRadioButtonDijkstra.isSelected()) {
                    Application.getGraphS().setCurrentAlgorithme(new AlgorithmeDijkstra());
                }
            }
        });

        jRadioButtonExploration.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                if (jRadioButtonExploration.isSelected()) {
                    Application.getGraphS().setCurrentAlgorithme(new AlgorithmeExplorationArborescente());
                }
            }
        });

        buttonGroupAlgorithmes.add(jRadioButtonBellman);
        buttonGroupAlgorithmes.add(jRadioButtonDijkstra);
        buttonGroupAlgorithmes.add(jRadioButtonExploration);

        jToolBarAlgorithmes.add(jRadioButtonBellman);
        jToolBarAlgorithmes.add(jRadioButtonDijkstra);
        jToolBarAlgorithmes.add(jRadioButtonExploration);
        jToolBarAlgorithmes.add(jButtonExecuteAlgorithme);
        jToolBarAlgorithmes.add(jButtonGenerateGraph);
        jToolBarAlgorithmes.add(jButtonSource);
        jToolBarAlgorithmes.add(jButtonDestination);
        jToolBarAlgorithmes.add(jButtonAfficheLigne);

        jRadioButtonBellman.setSelected(true);

        jToolBarLignes.setOrientation(SwingConstants.VERTICAL);
        for (final String ligne : Application.getGraphS().getArcs().keySet()) {
            if (!ligne.equals("0")) {
                final JCheckBox jCheckBox = new JCheckBox(ligne);
                jCheckBox.setSelected(true);
                jToolBarLignes.add(jCheckBox);

                jCheckBox.addChangeListener(new ChangeListener() {
                    @Override
                    public void stateChanged(ChangeEvent e) {
                        boolean drawing = !jCheckBox.isSelected();

                        Application.getGraphS().afficheLigne(ligne);

                        for (Arc arc : Application.getGraphS().getArcs().get(ligne)) {
                            arc.getArcDrawable().setDrawing(drawing);
                            arc.getSource().getNoeudDrawable().setDrawing(drawing);
                            arc.getDestination().getNoeudDrawable().setDrawing(drawing);
                        }

                        Application.getWindowS().getjCanvas().repaint();
                    }
                });
            }
        }

        jToolBarHistory.setOrientation(SwingConstants.VERTICAL);

        pane.setLayout(bord);
        pane.add("North", jToolBarAlgorithmes);
        pane.add("Center", component);
        pane.add("West", Application.getWindowS().getjToolBarHistory());
        pane.add("East", scrollPaneLignes);

        frame.getContentPane().add(pane);
        frame.pack();

        frame.setVisible(true);
//              frame.setResizable(false);

    }

    public static void showOnFrame(Component component, String frameName) {
        Frame frame = new Frame(frameName);
        WindowAdapter wa = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        };
        //        frame.setSize(dim);
        frame.addWindowListener(wa);
        frame.add(component);
        frame.pack();

        frame.setVisible(true);
//              frame.setResizable(false);

    }

    public static void add2History(String history) {
        JToolBar jToolBarHistory = Application.getWindowS().getjToolBarHistory();
        JTextPane jTextPane = new JTextPane();
        jTextPane.setText(history);
        jToolBarHistory.add(jTextPane);
        jToolBarHistory.updateUI();
        System.out.println(history);
    }
}
